import numpy as np
from sklearn.model_selection import train_test_split
import pandas as pd
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
from matplotlib import pyplot
from sklearn.preprocessing import StandardScaler

# lsi_matrix = np.loadtxt(open("../model/all_lsi.csv","rb"),delimiter=",",skiprows=0)
lsi_matrix = np.loadtxt(open("../model/keyword_word2vec.csv","rb"),delimiter=",",skiprows=0)
# lsi_matrix = np.load('../model/doc_vectors_2row.npy')
df_train = pd.read_csv('../data/train_processed.csv')

X = lsi_matrix[0:4774, :]
Y = df_train['label'].values

# 数据标准化
# ss_X = StandardScaler()
# X = ss_X.fit_transform(X)
def fit_grid_point_RBF(C, gamma, X_train, y_train, X_val, y_val, pre_result):
    # 在训练集是那个利用SVC训练
    svc_model = SVC(C=C, kernel='rbf', gamma=gamma, probability=1)
    svc_model = svc_model.fit(X_train, y_train)
    prdict = svc_model.predict_proba(X_val)
    prdict_result = pre_result + prdict
    # 在校验集上返回accuracy
    accuracy = svc_model.score(X_val, y_val)

    # print("accuracy: %f, C：%f，gamma：%s" % (accuracy, C, gamma))
    return accuracy, prdict_result
# C_s = np.logspace(-1, 1, 3)# logspace(a,b,N)把10的a次方到10的b次方区间分成N份
# gamma_s = np.logspace(-2, 2, 5)

for j in range(10):
    X_train_1, X_val_1, y_train_1, y_val_1 = train_test_split(X, Y, test_size=0.10, random_state=j)
    prdict_result = np.zeros((len(y_val_1), 11), dtype=np.float64)




    for i in range(20):
        seed = i
        np.random.seed(seed)
        X_train, X_val, y_train, y_val = train_test_split(X_train_1, y_train_1, test_size=0.10, random_state=seed)

        C_s = [11]
        gamma_s = [0.02]

        accuracy_s = []
        for i, oneC in enumerate(C_s):
            for j, gamma in enumerate(gamma_s):
                tmp, prdict_result = fit_grid_point_RBF(oneC, gamma, X_train, y_train, X_val_1, y_val_1, prdict_result)
                accuracy_s.append(tmp)

    final_predict = np.argmax(prdict_result, axis=1) + 1
    print ("Accuracy of val: %f"%accuracy_score(y_val_1, final_predict))

# accuracy_s1 =np.array(accuracy_s).reshape(len(C_s),len(gamma_s))
# x_axis = np.log10(C_s)
# for j, gamma in enumerate(gamma_s):
#     pyplot.plot(x_axis, np.array(accuracy_s1[:,j]), label = ' Test - log(gamma)' + str(gamma))
#
# pyplot.legend()
# pyplot.xlabel( 'log(C)' )
# pyplot.ylabel( 'accuracy' )
# pyplot.savefig('RBF_SVM.png' )

# pyplot.show()
# 最佳参数是C:12,gamma:0.007